package com.yw.atp.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yw.atp.Constant;
import com.yw.atp.util.DateUtil;

@Entity
@Table(name=Constant.DB_PREFIX+"apps")
public class Apps implements IDEntity{
	private static final long serialVersionUID = -5542967154574739053L;
	@Id
    @GeneratedValue
	private Integer id;
	private String appid;
	private String appName;
	private String appNameEn;
	private String orderId;
	private String apkName;
	private String packageName;
	private String className;
	private String iconName;
	private String status = "0";
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdAt;
	@Temporal(TemporalType.TIMESTAMP)
	private Date updateAt;

	@ManyToOne
	private Group groupid;
	
	public Group getGroupid() {
		return groupid;
	}

	public void setGroupid(Group groupid) {
		this.groupid = groupid;
	}

	public Date getCreatedAt() {
		return createdAt;
	}
	
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	
	public Date getUpdateAt() {
		return updateAt;
	}
	
	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}
	
	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getAppNameEn() {
		return appNameEn;
	}

	public void setAppNameEn(String appNameEn) {
		this.appNameEn = appNameEn;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getApkName() {
		return apkName;
	}

	public void setApkName(String apkName) {
		this.apkName = apkName;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getIconName() {
		return iconName;
	}

	public void setIconName(String iconName) {
		this.iconName = iconName;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public static JSONArray capture(List<Apps> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Apps appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id", id);
		appm.put("appid",appid);
		appm.put("groupid",groupid.capture());
		appm.put("appName",appName);
		appm.put("appNameEn",appNameEn);
		appm.put("apkName",apkName);
		appm.put("packageName",packageName);
		appm.put("className",className);
		appm.put("iconName",iconName);
		appm.put("orderId",orderId);
		appm.put("createdAt",createdAt!=null?DateUtil.getServiceFormatTime(createdAt):" ");
		appm.put("updateAt",updateAt!=null?DateUtil.getServiceFormatTime(updateAt):" ");
		return appm;
	}
}
